#include "..\\include\Lib_Clara.h"
#include "..\\include\Dir.h"
#include "phone.h"
#include "..\\include\var_arg.h"

char *gdfs_area;
int gdfs_size;
int start_address;
int size;
int PhoneType = UNKNOWN;
void GuiCreateGuiList(void * r0, BOOK * bk, wchar_t * GUI_NAME, int ItemNumber);
int CreateGDFSBackup (void);


typedef struct
{
BOOK book;
GUI_LIST*gui;
GUI *yesno;
}MyBOOK;

MyBOOK * myBook;

void OnNoExitGui(BOOK * bk, void *)
{
  GUI_Free( myBook->yesno );
  GuiCreateGuiList(0,(BOOK*)myBook,L"Select phone type:",3);
}

void OnYesTurn(BOOK * bk, void *)
{
  GUI_Free( myBook->yesno );
  CreateGDFSBackup();
}

void CreateTurnQuestion(wchar_t* mess)
{
  myBook->yesno = CreateYesNoQuestionVA(0,
                                VAR_BOOK( myBook ),
                                VAR_YESNO_PRE_QUESTION( Str2ID( mess, 0, 0xffff) ),
                                VAR_YESNO_QUESTION( Str2ID( L"Correct?", 0, 0xffff) ),
                                VAR_YESNO_YES_ACTION( OnYesTurn ),
                                VAR_YESNO_NO_ACTION( OnNoExitGui ),
                                0);
}

int TerminateElf(void * ,BOOK* book)
{
  FreeBook(book);
  return(1);
}

typedef struct
{
  BOOK * book;
}MSG;

int ShowAuthorInfo(void *mess ,BOOK* book)
{
  MSG * msg = (MSG*)mess;
  MessageBox(0x6fFFFFFF,STR("EasyGDFSBackup v1.3\n(c)mc_kibel\n(r)den_po,ploik"),0, 1 ,5000,msg->book);
  return(1);
}

void GuiBack(BOOK *bk, void *)
{
  FreeBook(bk);
}

int isRunned(BOOK * book)
{
  if(!strcmp(book->xbook->name,"EasyGDFSBackup")) 
    return 1;
  
  return 0;
}

int CreateGDFSBackup (void)
{
    wchar_t buf[301];
    gdfs_area=(char*)(start_address); 
    gdfs_size = size;
    DATETIME dt;
    REQUEST_DATEANDTIME_GET (0, &dt); 
    wchar_t buf2[201];
    snwprintf (buf2, 200, L"GDFS-%02d-%02d-%02d-%02d-%02d-%02d", dt.time.hour, dt.time.min, dt.time.sec, dt.date.day, dt.date.mon, dt.date.year);
    
    int f = _fopen ( GetDir ( DIR_OTHER | MEM_INTERNAL ), buf2, WA_Write | WA_Append | WA_Create, 0x1FF, 0 );
    fwrite ( f, gdfs_area, gdfs_size );
    fclose ( f );
    snwprintf (buf,300,L"GDFS backup done at:\n0x%X\nSize:0x%X",start_address,size);
    int infoMSG = Str2ID (buf,0,SID_ANY_LEN);
    MessageBox (0x6Fffffff,infoMSG,0,1,3000,0);
    
    FreeBook(FindBook(isRunned));
    
    return 0;
}


void GuiOnEnterPressed(BOOK *bk, void *lt)
{     
  int item=ListMenu_GetSelectedItem(myBook->gui);
  
  if(item==0) //k600
  {
    start_address = 0x21F00000;
    size = 0x100000;
    CreateGDFSBackup ();
  }
  else if(item==1) //w550/w600/k750/w800
  {
    start_address = 0x45F00000;
    size = 0x100000;
    CreateGDFSBackup ();
  }
  else if (item==2) //db20xx
  {
    start_address = 0x47E00000;
    size = 0x200000;
    CreateGDFSBackup ();
  }
}

int GuiOnLBMessage(GUI_MESSAGE * msg)
{
  int item;  
  switch(msg->msg)
  {    
  case 1:
    item=GUIonMessage_GetCreatedItemIndex(msg);    
    if(item==0)
      SetMenuItemText0(msg,STR("K600"));
    else if(item==1)
      SetMenuItemText0(msg,STR("W550/W600/K750/W800")); 
    else if(item==2)
      SetMenuItemText0(msg,STR("DB201x/2020"));
    break;
  }
  return(1);
};

void GuiCreateGuiList(void * r0, BOOK * bk, wchar_t * GUI_NAME, int ItemNumber)
{  
  myBook->gui=CreateListObject(bk,0);
  GuiObject_SetTitleText(myBook->gui,Str2ID(GUI_NAME,0,SID_ANY_LEN));
  SetNumOfMenuItem(myBook->gui,ItemNumber);
  OneOfMany_SetonMessage((GUI_ONEOFMANY*)myBook->gui,GuiOnLBMessage);
  SetCursorToItem(myBook->gui,0);
  SetMenuItemStyle(myBook->gui,0);
  GUIObject_Softkey_SetAction(myBook->gui,ACTION_BACK, GuiBack);  
  GUIObject_Softkey_SetAction(myBook->gui,ACTION_LONG_BACK, GuiBack);  
  GUIObject_Softkey_SetAction(myBook->gui,ACTION_SELECT1,GuiOnEnterPressed);     
  ShowWindow(myBook->gui);  
};

const PAGE_MSG HW_PageEvents[]@ "DYN_PAGE" ={
  ELF_TERMINATE_EVENT , TerminateElf,
  ELF_SHOW_INFO_EVENT  , ShowAuthorInfo,
  NIL_EVENT_TAG,           NULL
};
PAGE_DESC base_page ={"HW_BasePage",0,HW_PageEvents};


void elf_exit(void)
{
  kill_data(&ELF_BEGIN, (void(*)(void*))mfree_adr());
}

void onCloseHWBook(BOOK * book)
{
  if (book)
  {
    SUBPROC(elf_exit);
  }
}

MyBOOK * CreateHWBook()
{
  myBook= new MyBOOK;  
  CreateBook(myBook,onCloseHWBook,&base_page,"EasyGDFSBackup",-1,0);
  return(myBook);
}

int GetPhoneType()
{
  char tmp[8];
  for(int i=0; i<phoneNums; i++)
  {
    if(PhoneDB[i].addr)
    {
      memcpy(tmp,PhoneDB[i].addr,7);
      tmp[7] = 0;
      if(strcmp(tmp,PhoneDB[i].firmware) == 0)
      {
        return PhoneDB[i].type;
      }
    }
  }
  return UNKNOWN;
}

int main (void)
{
  CreateHWBook();
  
  PhoneType = GetPhoneType();
  if(PhoneType == K750_R1CA021 || PhoneType == W800_R1BD001 || PhoneType == W700_R1CA021)
  {
    start_address = 0x45F00000;
    size = 0x100000;
    CreateTurnQuestion(L"Detected: K750/W800/W700");
  }
  else if(PhoneType == W550_R4CB020 || PhoneType == W600_R7CA017)
  {
    start_address = 0x45F00000;
    size = 0x100000;
    CreateTurnQuestion(L"Detected: W550/W600");
  }
  else
  {
    start_address = 0x47E00000;
    size = 0x200000;
    CreateTurnQuestion(L"Detected: others DB201x/DB2020");
    
  }
  
  
  return(0);
}
